package com.renjianbo.project.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.renjianbo.project.common.model.PageReq;
import com.renjianbo.project.module.system.mapper.SysRolePermissionMapper;
import com.renjianbo.project.module.system.model.sys_role_permission.SysRolePermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import com.renjianbo.project.module.system.model.sys_permission.*;
import com.renjianbo.project.module.system.mapper.SysPermissionMapper;
import com.renjianbo.project.module.system.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.renjianbo.project.common.model.PageInfo;
import com.renjianbo.project.common.util.JsonUtil;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2022-04-19
 */
@Service
@Slf4j
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 同步权限
     */
    @Override
    @Transactional
    public void syncPermission(List<SysPermissionSyncReq> permissionSyncFormList){
        sysPermissionMapper.delete( null);

        savePermission(permissionSyncFormList, 0L, "menu");
    }

    /**
    * 查询数据
    */
    @Override
    public List<SysPermissionDTO> list(SysPermissionSearchReq req) {
        List<SysPermissionDTO> list = sysPermissionMapper.list(req);

        return list;
    }

    /**
    * 根据id查询
    */
    @Override
    public SysPermissionDTO getById(String id){
        SysPermissionDTO sysPermission = sysPermissionMapper.getById(id);

        return sysPermission;
    }


    /**
     *  分配权限
     */
    @Override
    @Transactional
    public void assignsPermission(Long roleId, Long[] permissionIds) {
        //先删除角色权限关联表
        LambdaUpdateWrapper<SysRolePermission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRolePermission::getRoleId, roleId);
        sysRolePermissionMapper.delete(updateWrapper);

        //添加角色权限关联表
        if(permissionIds != null) {
            for(Long permissionId: permissionIds) {
                SysRolePermission rolePermission = new SysRolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                sysRolePermissionMapper.insert(rolePermission);
            }
        }
    }

    /**
     * 保存菜单权限
     */
    public void savePermission(List<SysPermissionSyncReq> permissionSyncFormList, Long pid, String permissionType) {
        for (int i = 0; i < permissionSyncFormList.size(); i++) {
            SysPermissionSyncReq sysPermissionI = permissionSyncFormList.get(i);
            SysPermission sysPermission = new SysPermission();


            sysPermission.setId(sysPermissionI.getId());
            sysPermission.setPid(pid);
            //  功能类型为窗口页面
            if(sysPermissionI.getType()!= null) {
                sysPermission.setPermissionType(sysPermissionI.getType().trim());
            //  功能类型为显示控制
            } else {
                sysPermission.setPermissionType(permissionType.trim());
            }
            sysPermission.setPermissionCode(sysPermissionI.getPermissionCode().replaceAll("\\s*|\r|\n|\t", "").trim());
            sysPermission.setPermissionName(sysPermissionI.getPermissionName().replaceAll("\\s*|\r|\n|\t", "").trim());
            sysPermission.setSort(i);
            sysPermissionMapper.insert(sysPermission);

            //  保存菜单权限
            if (sysPermissionI.getChildren() != null) {
                savePermission(sysPermissionI.getChildren(), sysPermission.getId(), "menu");
            }
            //  保存功能权限
            if (sysPermissionI.getFuncs() != null) {
                savePermission(sysPermissionI.getFuncs(), sysPermission.getId(), "func");
            }
        }
    }

}
